<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>对于ES6模块与CommonJS模块理解 | Zelma&#39;s Blog</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/logo.png">
    <meta name="description" content="记录，成为更好的自己！">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.4cf3e643.css" as="style"><link rel="preload" href="/assets/js/app.6bff9ace.js" as="script"><link rel="preload" href="/assets/js/4.4f19eb35.js" as="script"><link rel="preload" href="/assets/js/1.085d100d.js" as="script"><link rel="preload" href="/assets/js/36.245a9409.js" as="script"><link rel="prefetch" href="/assets/js/10.a9f8c063.js"><link rel="prefetch" href="/assets/js/11.104a9c15.js"><link rel="prefetch" href="/assets/js/12.2b5515f3.js"><link rel="prefetch" href="/assets/js/13.4b350042.js"><link rel="prefetch" href="/assets/js/14.c2117806.js"><link rel="prefetch" href="/assets/js/15.f122a822.js"><link rel="prefetch" href="/assets/js/16.b7b77918.js"><link rel="prefetch" href="/assets/js/17.ca19e9d9.js"><link rel="prefetch" href="/assets/js/18.86eacc78.js"><link rel="prefetch" href="/assets/js/19.a9eff237.js"><link rel="prefetch" href="/assets/js/20.1cc397e3.js"><link rel="prefetch" href="/assets/js/21.543a015c.js"><link rel="prefetch" href="/assets/js/22.319e4a6e.js"><link rel="prefetch" href="/assets/js/23.e60f5f5b.js"><link rel="prefetch" href="/assets/js/24.b3891c01.js"><link rel="prefetch" href="/assets/js/25.ed2e6b36.js"><link rel="prefetch" href="/assets/js/26.40d7e552.js"><link rel="prefetch" href="/assets/js/27.d5e7e0d7.js"><link rel="prefetch" href="/assets/js/28.10aa572b.js"><link rel="prefetch" href="/assets/js/29.acf9222c.js"><link rel="prefetch" href="/assets/js/3.74a0f1d8.js"><link rel="prefetch" href="/assets/js/30.ab8bfb83.js"><link rel="prefetch" href="/assets/js/31.36528513.js"><link rel="prefetch" href="/assets/js/32.f582544c.js"><link rel="prefetch" href="/assets/js/33.41a28fb2.js"><link rel="prefetch" href="/assets/js/34.73610167.js"><link rel="prefetch" href="/assets/js/35.f1ec0595.js"><link rel="prefetch" href="/assets/js/37.c96cef21.js"><link rel="prefetch" href="/assets/js/38.ef989ff8.js"><link rel="prefetch" href="/assets/js/39.b6ac0196.js"><link rel="prefetch" href="/assets/js/40.f64953b6.js"><link rel="prefetch" href="/assets/js/5.c1ba3938.js"><link rel="prefetch" href="/assets/js/6.5bb669f5.js"><link rel="prefetch" href="/assets/js/7.0bd32045.js"><link rel="prefetch" href="/assets/js/8.a1032cf5.js"><link rel="prefetch" href="/assets/js/9.4f6fb2c7.js">
    <link rel="stylesheet" href="/assets/css/0.styles.4cf3e643.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-1aefc0b4><div data-v-1aefc0b4><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1aefc0b4 data-v-1aefc0b4><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Zelma's Blog</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>记录，成为更好的自己！</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>zelma</span>
            
          <!---->
          2022
        </a></span></div></div> <div class="hide" data-v-1aefc0b4><header class="navbar" data-v-1aefc0b4><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="Zelma's Blog" class="logo"> <span class="site-name">Zelma's Blog</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/随记/" class="nav-link"><i class="undefined"></i>
  随记
</a></li><li class="dropdown-item"><!----> <a href="/categories/工具/" class="nav-link"><i class="undefined"></i>
  工具
</a></li><li class="dropdown-item"><!----> <a href="/categories/JS设计模式/" class="nav-link"><i class="undefined"></i>
  JS设计模式
</a></li><li class="dropdown-item"><!----> <a href="/categories/git使用/" class="nav-link"><i class="undefined"></i>
  git使用
</a></li><li class="dropdown-item"><!----> <a href="/categories/业务场景交互具体实现/" class="nav-link"><i class="undefined"></i>
  业务场景交互具体实现
</a></li><li class="dropdown-item"><!----> <a href="/categories/技术分享/" class="nav-link"><i class="undefined"></i>
  技术分享
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      全部文档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/theme-reco/js/prototype.html" class="nav-link"><i class="undefined"></i>
  JS相关知识
</a></li><li class="dropdown-item"><!----> <a href="/docs/theme-reco/interactive/reactVr.html" class="nav-link"><i class="undefined"></i>
  交互具体实现
</a></li><li class="dropdown-item"><!----> <a href="/docs/theme-reco/designPattern/preface.html" class="nav-link"><i class="undefined"></i>
  JavaScript设计模式与开发实践
</a></li><li class="dropdown-item"><!----> <a href="/docs/theme-reco/git/gitNormal.html" class="nav-link"><i class="undefined"></i>
  Git学习
</a></li></ul></div></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-other"></i>
      工具
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs-tools/uploadImg.html" class="nav-link"><i class="undefined"></i>
  上传图片
</a></li></ul></div></div><div class="nav-item"><a href="/blogs/other/messageBoard.html" class="nav-link"><i class="iconfont reco-date"></i>
  留言板
</a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1aefc0b4></div> <aside class="sidebar" data-v-1aefc0b4><div class="personal-info-wrapper" data-v-39576ba9 data-v-1aefc0b4><img src="/avatar.png" alt="author-avatar" class="personal-img" data-v-39576ba9> <h3 class="name" data-v-39576ba9>
    zelma
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>25</h3> <h6 data-v-39576ba9>文章</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>6</h3> <h6 data-v-39576ba9>标签</h6></div></div> <ul class="social-links" data-v-39576ba9><li class="social-item" data-v-39576ba9><i class="iconfont reco-juejin" style="color:#f8b26a;" data-v-39576ba9></i></li><li class="social-item" data-v-39576ba9><i class="iconfont reco-mayun" style="color:#f26d6d;" data-v-39576ba9></i></li><li class="social-item" data-v-39576ba9><i class="iconfont reco-csdn" style="color:#f47e60;" data-v-39576ba9></i></li></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/随记/" class="nav-link"><i class="undefined"></i>
  随记
</a></li><li class="dropdown-item"><!----> <a href="/categories/工具/" class="nav-link"><i class="undefined"></i>
  工具
</a></li><li class="dropdown-item"><!----> <a href="/categories/JS设计模式/" class="nav-link"><i class="undefined"></i>
  JS设计模式
</a></li><li class="dropdown-item"><!----> <a href="/categories/git使用/" class="nav-link"><i class="undefined"></i>
  git使用
</a></li><li class="dropdown-item"><!----> <a href="/categories/业务场景交互具体实现/" class="nav-link"><i class="undefined"></i>
  业务场景交互具体实现
</a></li><li class="dropdown-item"><!----> <a href="/categories/技术分享/" class="nav-link"><i class="undefined"></i>
  技术分享
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      全部文档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/theme-reco/js/prototype.html" class="nav-link"><i class="undefined"></i>
  JS相关知识
</a></li><li class="dropdown-item"><!----> <a href="/docs/theme-reco/interactive/reactVr.html" class="nav-link"><i class="undefined"></i>
  交互具体实现
</a></li><li class="dropdown-item"><!----> <a href="/docs/theme-reco/designPattern/preface.html" class="nav-link"><i class="undefined"></i>
  JavaScript设计模式与开发实践
</a></li><li class="dropdown-item"><!----> <a href="/docs/theme-reco/git/gitNormal.html" class="nav-link"><i class="undefined"></i>
  Git学习
</a></li></ul></div></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-other"></i>
      工具
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs-tools/uploadImg.html" class="nav-link"><i class="undefined"></i>
  上传图片
</a></li></ul></div></div><div class="nav-item"><a href="/blogs/other/messageBoard.html" class="nav-link"><i class="iconfont reco-date"></i>
  留言板
</a></div> <!----></nav> <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>JS相关知识</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/docs/theme-reco/js/prototype.html" class="sidebar-link">JS原型链</a></li><li><a href="/docs/theme-reco/js/object.html" class="sidebar-link">对象的方法总结</a></li><li><a href="/docs/theme-reco/js/array.html" class="sidebar-link">数组的方法总结</a></li><li><a href="/docs/theme-reco/js/flatArray.html" class="sidebar-link">扁平化数组的n种方式</a></li><li><a href="/docs/theme-reco/js/module.html" aria-current="page" class="active sidebar-link">对于ES6模块与CommonJS模块理解</a></li><li><a href="/docs/theme-reco/js/defineProperty.html" class="sidebar-link">defineProperty实现数据劫持</a></li><li><a href="/docs/theme-reco/js/onEmit.html" class="sidebar-link">发布订阅模式理解与应用</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>业务场景交互具体实现</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>JavaScript设计模式与开发实践</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Git学习</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>对于ES6模块与CommonJS模块理解</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>zelma</span>
            
          <!---->
          2022
        </a></span></div></div> <div data-v-1aefc0b4><main class="page"><section><div class="page-title"><h1 class="title">对于ES6模块与CommonJS模块理解</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>zelma</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>2022/8/13</span></i> <i class="iconfont reco-eye" data-v-f875f3fc><span id="/docs/theme-reco/js/module.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-f875f3fc><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>js</span></i></div></div> <div class="theme-reco-content content__default"><h1 id="es6-模块与-commonjs-模块理解"><a href="#es6-模块与-commonjs-模块理解" class="header-anchor">#</a> ES6 模块与 CommonJS 模块理解</h1> <h2 id="commonjs-理解"><a href="#commonjs-理解" class="header-anchor">#</a> CommonJS 理解</h2> <p>CommonJS 就是为 JS 的表现来制定规范，因为 js 没有模块的功能所以 CommonJS 应运而生，它希望 js 可以在任何地方运行，不只是浏览器中。通常使用在 nodeJs 中。
使用 exports module 或 exports 进行模块导出，使用 require 进行模块的导入</p> <h3 id="exports-和-exports-module-区别"><a href="#exports-和-exports-module-区别" class="header-anchor">#</a> exports 和 exports module 区别</h3> <p>为了方便，Node 为每个模块提供一个 exports 变量，指向 module.exports。这等同在每个模块头部，有一行这样的命令</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">const</span> exports <span class="token operator">=</span> module<span class="token punctuation">.</span>exports<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>不能将 exports 和 module.exports 重新赋值，如果重新赋值不会生效的 如下面这几种情况，</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function-variable function">exports</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token function">fn</span><span class="token punctuation">(</span><span class="token parameter">a</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">// 不能直接将exports变量指向一个值，因为这样等于切断了exports与module.exports的联系</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// module.exports被重新赋值了， exports与module.exports联系也没有了，fn函数不会被导出</span>
exports<span class="token punctuation">.</span><span class="token function-variable function">fn</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">a</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token string">&quot;hello word&quot;</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>正确做法应该是这样子</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// common.js</span>
<span class="token keyword">let</span> <span class="token function-variable function">fn</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">a</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> str <span class="token operator">=</span> <span class="token string">&quot;hello word&quot;</span><span class="token punctuation">;</span>
module<span class="token punctuation">.</span>exports<span class="token punctuation">.</span>str <span class="token operator">=</span> str<span class="token punctuation">;</span>
exports<span class="token punctuation">.</span>fn <span class="token operator">=</span> fn<span class="token punctuation">;</span> <span class="token comment">// exports = module.exports 代码中可以只用一种即可</span>

<span class="token comment">// index.js</span>
<span class="token keyword">const</span> common <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;./common&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>common<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// { str: 'hello word', fn: [Function: fn] }</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>注意事项：<strong>package.json 中不能设置 type 为 module，否则会报错</strong>，原因是设置为 module 后，需要使用 ES6 的模块化方案</p> <h3 id="模块的缓存"><a href="#模块的缓存" class="header-anchor">#</a> 模块的缓存</h3> <p>第一次加载某个模块时，Node 会缓存该模块。以后再加载该模块，就直接从缓存取出该模块的 module.exports 属性</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// common.js</span>
module<span class="token punctuation">.</span>exports<span class="token punctuation">.</span>a <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>

<span class="token comment">// index.js</span>
<span class="token keyword">let</span> common <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;./common&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
common<span class="token punctuation">.</span>b <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> common1 <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;./common&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>common1<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// { a: 1, b: 2 }</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>所有缓存的模块保存在 require.cache 之中，如果想删除模块的缓存，可以像下面这样写。</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 删除指定模块的缓存</span>
<span class="token keyword">delete</span> require<span class="token punctuation">.</span>cache<span class="token punctuation">[</span>moduleName<span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token comment">// 删除所有模块的缓存</span>
Object<span class="token punctuation">.</span><span class="token function">keys</span><span class="token punctuation">(</span>require<span class="token punctuation">.</span>cache<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">delete</span> require<span class="token punctuation">.</span>cache<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>总结：</p> <ul><li>所有代码运行在模块作用域，不会污染全局作用域</li> <li>模块可以多次加载，但是只会在第一次加载时运行一次，然后运行结果就被缓存了，以后再加载，就直接读取缓存结果。要想让模块再次运行，必须清除缓存。</li> <li>模块加载的顺序，按照其在代码中出现的顺序</li></ul> <h2 id="es6-模块化理解"><a href="#es6-模块化理解" class="header-anchor">#</a> ES6 模块化理解</h2> <p>ES6 模块的设计思想是尽量的静态化，使得编译时就能确定模块的依赖关系，以及输入和输出的变量,像 CommonJS 模块是在运行时加载，相比较 ES6 模块，就会效率没那么高。</p> <ul><li>ES6 模块必须用 export 导出</li> <li>export 必须与模块内部的变量建立一一对应关系</li></ul> <h3 id="如何使用-es6-模块"><a href="#如何使用-es6-模块" class="header-anchor">#</a> 如何使用 ES6 模块</h3> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 首先需要建立初始化package.json文件</span>
  npm init <span class="token operator">-</span>y
<span class="token comment">// 在package.json中设置type:&quot;module&quot;</span>
  <span class="token string-property property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;practice&quot;</span><span class="token punctuation">,</span>
  <span class="token string-property property">&quot;version&quot;</span><span class="token operator">:</span> <span class="token string">&quot;1.0.0&quot;</span><span class="token punctuation">,</span>
  <span class="token string-property property">&quot;main&quot;</span><span class="token operator">:</span> <span class="token string">&quot;index.js&quot;</span><span class="token punctuation">,</span>
  <span class="token string-property property">&quot;type&quot;</span><span class="token operator">:</span> <span class="token string">&quot;module&quot;</span><span class="token punctuation">,</span> <span class="token comment">// 这个</span>
  <span class="token string-property property">&quot;devDependencies&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token string-property property">&quot;scripts&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token string-property property">&quot;test&quot;</span><span class="token operator">:</span> <span class="token string">&quot;echo \&quot;Error: no test specified\&quot; &amp;amp;&amp;amp; exit 1&quot;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token string-property property">&quot;keywords&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token string-property property">&quot;author&quot;</span><span class="token operator">:</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span>
  <span class="token string-property property">&quot;license&quot;</span><span class="token operator">:</span> <span class="token string">&quot;ISC&quot;</span><span class="token punctuation">,</span>
  <span class="token string-property property">&quot;description&quot;</span><span class="token operator">:</span> <span class="token string">&quot;&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h3 id="export-和-export-default-的区别"><a href="#export-和-export-default-的区别" class="header-anchor">#</a> export 和 export default 的区别</h3> <ul><li>export 可以多次的出现，export default 只能出现一次</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">const</span> str <span class="token operator">=</span> <span class="token string">'123'</span>
<span class="token keyword">export</span> <span class="token keyword">let</span> <span class="token function-variable function">fn</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">a</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">}</span>

<span class="token keyword">let</span> b <span class="token operator">=</span> <span class="token string">'456'</span>
<span class="token keyword">let</span> c <span class="token operator">=</span> <span class="token string">'789'</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> b
<span class="token keyword">export</span> <span class="token keyword">default</span> c <span class="token comment">// error 报错</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ul><li>export 命令规定的是对外的接口，必须与模块内部的变量建立一一对应关系</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 报错</span>
<span class="token keyword">export</span> <span class="token number">1</span><span class="token punctuation">;</span>

<span class="token comment">// 报错</span>
<span class="token keyword">const</span> m <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token keyword">export</span> m<span class="token punctuation">;</span>

<span class="token comment">// 正确写法</span>
<span class="token keyword">export</span> cont a <span class="token operator">=</span> <span class="token number">1</span>
<span class="token keyword">const</span> m <span class="token operator">=</span> <span class="token number">1</span>
<span class="token keyword">export</span> <span class="token punctuation">{</span>m<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><ul><li>export default 就是输出一个叫做 default 的变量或方法,所以后面不能直接跟变量的声明语句</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 错误书写</span>
<span class="token keyword">export</span> deafult <span class="token keyword">const</span> a <span class="token operator">=</span> <span class="token number">1</span> <span class="token comment">// 报错</span>

<span class="token comment">// 正确书写</span>
<span class="token keyword">let</span> a <span class="token operator">=</span> <span class="token number">1</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> a <span class="token comment">// 导入时直接为1</span>
<span class="token comment">// 或</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
	a <span class="token comment">//导入时为{ a: 1 }</span>
<span class="token punctuation">}</span>

<span class="token comment">// 因为直接就是导出default的变量所以可以直接跟值</span>
expore deafult <span class="token number">1</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h3 id="import-的使用"><a href="#import-的使用" class="header-anchor">#</a> import 的使用</h3> <ul><li>import 命令输入的变量都是只读的</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// util.js</span>
<span class="token keyword">export</span> <span class="token keyword">let</span> a <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token comment">// index.js</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> a <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;./util.js&quot;</span><span class="token punctuation">;</span>
a <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token literal-property property">foo</span><span class="token operator">:</span> <span class="token number">1</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">// 报错</span>
a<span class="token punctuation">.</span>foo <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// { foo: 1 }</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><ul><li>import 命令具有提升效果</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// util.js</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// index.js</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> fn <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;./util.js&quot;</span><span class="token punctuation">;</span>
<span class="token function">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 打印顺序 2 1 3</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><ul><li>import 是静态执行，所以不能使用表达式和变量</li> <li>import 语句是 Singleton 模式</li></ul> <h4 id="不同的接收方式"><a href="#不同的接收方式" class="header-anchor">#</a> 不同的接收方式</h4> <ul><li>接受 export 导出的模块</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// util.js</span>
<span class="token keyword">export</span> <span class="token keyword">const</span> a <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token keyword">export</span> <span class="token keyword">let</span> <span class="token function-variable function">fn</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">a</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token comment">// 或</span>
<span class="token keyword">const</span> a <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> <span class="token function-variable function">fn</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">a</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">export</span> <span class="token punctuation">{</span> a<span class="token punctuation">,</span> fn <span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token comment">// index.js</span>
<span class="token comment">// 导入模块名的名字必须与导出的名字相同</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> a<span class="token punctuation">,</span> fn <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;./util.js&quot;</span><span class="token punctuation">;</span>
<span class="token comment">// 可以使用 as 来重命名</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> a <span class="token keyword">as</span> b<span class="token punctuation">,</span> fn <span class="token keyword">as</span> fn1 <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;./util.js&quot;</span><span class="token punctuation">;</span>
<span class="token comment">// 可以使用 * 来把所有都出的模块合成一个对象</span>
<span class="token keyword">import</span> <span class="token operator">*</span> <span class="token keyword">as</span> data <span class="token keyword">from</span> <span class="token string">&quot;./utils&quot;</span><span class="token punctuation">;</span> <span class="token comment">// { a: 1, fn: function }</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>-接受 export defalut 导出的模块</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// util.js</span>
<span class="token keyword">const</span> a <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> <span class="token function-variable function">fn</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">a</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
  a<span class="token punctuation">,</span>
  fn<span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token comment">// index.js</span>
<span class="token comment">// 导入时的可以随便取</span>
<span class="token keyword">import</span> util <span class="token keyword">from</span> <span class="token string">&quot;./util.js&quot;</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>util<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// { a: 1, fn: function }</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>-同时接受 export 和 export default 导出的模块时用各自的接受方式接受即可，也可以使用* as data 把所有模块都放在 data 里面</p> <h3 id="es6-模块与-commonjs-模块区别"><a href="#es6-模块与-commonjs-模块区别" class="header-anchor">#</a> ES6 模块与 CommonJS 模块区别</h3> <ul><li>CommonJs 模块输出的是一个值的拷贝，只是一个浅拷贝的过程，如果改变的是对象里面的值，还是会改变的。</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// common.js</span>
<span class="token keyword">let</span> a <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> <span class="token function-variable function">fn</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  a<span class="token operator">++</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
module<span class="token punctuation">.</span>exports<span class="token punctuation">.</span>a <span class="token operator">=</span> a<span class="token punctuation">;</span>
module<span class="token punctuation">.</span>exports<span class="token punctuation">.</span>fn <span class="token operator">=</span> fn<span class="token punctuation">;</span>

<span class="token comment">// index.js</span>
<span class="token keyword">let</span> common <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;./common&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>common<span class="token punctuation">.</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 1</span>
common<span class="token punctuation">.</span><span class="token function">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>common<span class="token punctuation">.</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 1</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><ul><li>ES6 模块输出的是值的引用。</li></ul> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// util.js</span>
<span class="token keyword">export</span> <span class="token keyword">let</span> a <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token keyword">export</span> <span class="token keyword">let</span> <span class="token function-variable function">fu</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  a<span class="token operator">++</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token comment">// index.js</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> a<span class="token punctuation">,</span> fn <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">&quot;./util.js&quot;</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 1</span>
<span class="token function">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 2</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><ul><li>CommonJs 模块是运行时加载，ES6 模块是编译时输出接口。
因为 CommonJS 加载的是一个对象（即 module.exports 属性）。该对象只有在脚本运行完才会生成。而 ES6 模块不是对象，它的对外接口只是一种静态定义，在代码静态编译阶段就会生成</li></ul></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">2022/8/13 下午1:52:23</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/docs/theme-reco/js/flatArray.html" class="prev">
            扁平化数组的n种方式
          </a></span> <span class="next"><a href="/docs/theme-reco/js/defineProperty.html">
            defineProperty实现数据劫持
          </a></span></p></div> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/docs/theme-reco/js/module.html#commonjs-理解" class="sidebar-link reco-side-commonjs-理解" data-v-cb1513f6>CommonJS 理解</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/theme-reco/js/module.html#exports-和-exports-module-区别" class="sidebar-link reco-side-exports-和-exports-module-区别" data-v-cb1513f6>exports 和 exports module 区别</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/theme-reco/js/module.html#模块的缓存" class="sidebar-link reco-side-模块的缓存" data-v-cb1513f6>模块的缓存</a></li><li class="level-2" data-v-cb1513f6><a href="/docs/theme-reco/js/module.html#es6-模块化理解" class="sidebar-link reco-side-es6-模块化理解" data-v-cb1513f6>ES6 模块化理解</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/theme-reco/js/module.html#如何使用-es6-模块" class="sidebar-link reco-side-如何使用-es6-模块" data-v-cb1513f6>如何使用 ES6 模块</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/theme-reco/js/module.html#export-和-export-default-的区别" class="sidebar-link reco-side-export-和-export-default-的区别" data-v-cb1513f6>export 和 export default 的区别</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/theme-reco/js/module.html#import-的使用" class="sidebar-link reco-side-import-的使用" data-v-cb1513f6>import 的使用</a></li><li class="level-3" data-v-cb1513f6><a href="/docs/theme-reco/js/module.html#es6-模块与-commonjs-模块区别" class="sidebar-link reco-side-es6-模块与-commonjs-模块区别" data-v-cb1513f6>ES6 模块与 CommonJS 模块区别</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><canvas id="vuepress-canvas-cursor"></canvas><div class="kanbanniang" data-v-5775ee02><div class="banniang-container" style="display:;" data-v-5775ee02><div class="messageBox" style="right:68px;bottom:290px;display:none;" data-v-5775ee02>
      欢迎来到Zelma的博客
    </div> <div class="operation" style="right:90px;bottom:40px;display:;" data-v-5775ee02><i class="kbnfont kbn-ban-home ban-home" data-v-5775ee02></i> <i class="kbnfont kbn-ban-message message" data-v-5775ee02></i> <i class="kbnfont kbn-ban-close close" data-v-5775ee02></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-5775ee02><i class="kbnfont kbn-ban-info info" data-v-5775ee02></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:;" data-v-5775ee02></i></div> <canvas id="banniang" width="250" height="320" class="live2d" style="right:90px;bottom:-20px;opacity:0.9;" data-v-5775ee02></canvas></div> <div class="showBanNiang" style="display:none;" data-v-5775ee02>
    看板娘
  </div></div><!----><div class="RibbonAnimation"></div></div></div>
    <script src="/assets/js/app.6bff9ace.js" defer></script><script src="/assets/js/4.4f19eb35.js" defer></script><script src="/assets/js/1.085d100d.js" defer></script><script src="/assets/js/36.245a9409.js" defer></script>
  </body>
</html>
